package com.platform.user.controller;

import com.platform.bean.PageInfo;
import com.platform.bean.Result;
import com.platform.bean.SimilarImageResult;
import com.platform.user.entity.Announcement;
import com.platform.user.entity.ItemClaimNotice;
import com.platform.user.entity.LostHelpNotice;
import com.platform.user.entity.User;
import com.platform.user.service.*;
import com.platform.utils.BaiduSimilarImageServerUtils;
import com.platform.utils.PrintMessageUtils;
import org.json.JSONException;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/tourist")
@RestController
public class TouristController {

    @Resource
    private AnnouncementService announcementService;
    @Resource
    private ItemClaimNoticeService itemClaimNoticeService;
    @Resource
    private LostHelpNoticeService lostHelpNoticeService;
    @Resource
    private MyUserDetailService userDetailService;

    @PostMapping("searchSimilarLostHelpNotice")
    public Result<Map<String, Object>> searchSimilarLostHelpNotice(@RequestBody Map<String, Object> params) throws JSONException, IOException {
        Map<String, Object> imageSimilarResp = BaiduSimilarImageServerUtils.searchSimilarImageBrief(params);
        Map<String, Object> resp = new HashMap<>();
        List<SimilarImageResult> similarImageResultList = (List<SimilarImageResult>) imageSimilarResp.get("similarImageResultList");
        List<LostHelpNotice> lostHelpNoticeList = new ArrayList<>();
        for (SimilarImageResult x: similarImageResultList) {
            lostHelpNoticeList.add(lostHelpNoticeService.queryOneLostHelpNoticeByItemId(Integer.valueOf(x.getBrief())));
        }
        resp.put("list", lostHelpNoticeList);
        resp.put("total", Integer.parseInt(imageSimilarResp.get("total").toString()));
        return Result.ok(resp);
    }

    @PostMapping("searchSimilarItemClaimNotice")
    public Result<Map<String, Object>> searchSimilarItemClaimNotice(@RequestBody Map<String, Object> params) throws JSONException, IOException {
        Map<String, Object> imageSimilarResp = BaiduSimilarImageServerUtils.searchSimilarImageBrief(params);
        Map<String, Object> resp = new HashMap<>();
        List<SimilarImageResult> similarImageResultList = (List<SimilarImageResult>) imageSimilarResp.get("similarImageResultList");
        List<ItemClaimNotice> itemClaimNoticeList = new ArrayList<>();
        for (SimilarImageResult x: similarImageResultList) {
            itemClaimNoticeList.add(itemClaimNoticeService.queryOneItemClaimNoticeByItemId(Integer.valueOf(x.getBrief())));
        }
        resp.put("list", itemClaimNoticeList);
        resp.put("total", Integer.parseInt(imageSimilarResp.get("total").toString()));
        return Result.ok(resp);
    }

    @PostMapping("touristQueryUserInfoById")
    public Result<Map<String, Object>> touristQueryUserInfoById(@RequestBody Map<String, Object> params) {
        User user = userDetailService.touristQueryUserInfoById(Integer.parseInt(params.get("userId").toString()));
        return Result.ok(user);
    }

    @PostMapping("queryLostHelpNoticeByPage")
    public Result<Map<String, Object>> queryLostHelpNoticeByPage(@RequestBody Map<String, Object> params) {
        PageInfo<LostHelpNotice> lostHelpNoticePageInfo = lostHelpNoticeService.queryLostHelpNoticeByPage(params);
        return Result.ok(lostHelpNoticePageInfo);
    }

    @PostMapping("queryItemClaimNoticeByPage")
    public Result<Map<String, Object>> queryItemClaimNoticeByPage(@RequestBody Map<String, Object> params) {
        PageInfo<ItemClaimNotice> itemClaimNoticePageInfo = itemClaimNoticeService.queryItemClaimNoticeByPage(params);
        return Result.ok(itemClaimNoticePageInfo);
    }

    @PostMapping("queryAnnouncementById")
    public Result<Announcement> queryAnnouncementById(@RequestBody Map<String, Object> params) {
        Announcement announcement = announcementService.queryById(Integer.parseInt(params.get("id").toString()));
        return Result.ok(announcement);
    }

    @PostMapping("queryAnnouncementPreviewByPage")
    public Result<Map<String, Object>> queryAnnouncementPreviewByPage() {
        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex", 1);
        params.put("pageSize", 10);
        params.put("ifTop", 1);
        List<Announcement> topAnnouncementList = announcementService.queryAnnouncementByPage(params).getList();
        params.remove("ifTop");
        params.put("pageSize", 3);
        params.put("type", "0");
        List<Announcement> announcementList1 = announcementService.queryAnnouncementByPage(params).getList();
        params.remove("type");
        params.put("type", "1");
        List<Announcement> announcementList2 = announcementService.queryAnnouncementByPage(params).getList();
        params.remove("type");
        params.put("type", "2");
        List<Announcement> announcementList3 = announcementService.queryAnnouncementByPage(params).getList();

        Map<String, Object> res = new HashMap<>();
        res.put("topAnnouncementList", topAnnouncementList);
        res.put("announcementList1", announcementList1);
        res.put("announcementList2", announcementList2);
        res.put("announcementList3", announcementList3);
        return Result.ok(res);
    }

    @PostMapping("queryAnnouncementByPage")
    public Result<PageInfo<Announcement>> queryAnnouncementByPage(@RequestBody Map<String, Object> params) {
        PageInfo<Announcement> announcementPageInfo = announcementService.queryAnnouncementByPage(params);
        return Result.ok(announcementPageInfo);
    }

    @PostMapping("addImage")
    public Result addImage(@RequestBody Map<String, Object> params) throws IOException, JSONException {
        PrintMessageUtils.traverseMap(params);
        Result result = Result.ok();
//        String imageUrl = "http://117.72.40.8/images/test-info-image1.png";
        int i = BaiduSimilarImageServerUtils.addImage(params);
        if (i == 216681) {
            result.setMessage("添加失败！图片已经存在库中，无法重复添加");
            result.setData(0);
        } else if (i == 200) {
            result.setMessage("添加成功！");
            result.setData(1);
        } else {
            result.setMessage("其它未知错误！百度云图库错误码为" + i);
            result.setData(-1);
        }
        return result;
    }
}
